﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using GestionPedago.EntityContext.EntityOPTools;
using Persistence.OPManager;
using GestionPedago.Persistence.OPManager;

namespace GestionPedago.EntityContext {

    partial class ECF : EntityObject {


        public DateTime? GetSuggestionDate(PlanningIndividuelFormation pif)
        {
            try
            {
                PlanningECF pe;
                IQueryable<PlanningECF> queryPe = EntityConnection.GetContext().PlanningECF;
               /* if (pif.Promotion != null)
                {*/
                queryPe = queryPe.Where(it => (it.CodePromotion == pif.CodePromotion && it.ECF.IdECF == this.IdECF));
                queryPe = queryPe.OrderBy(it => it.DateECF);
                pe = queryPe.FirstOrDefault();
                // si on trouve une planif d'ecf correspondant à la promo (marche uniquement pour les FC)
                if (pe != null)
                {
                    return pe.DateECF;
                }
                else
                {
                    // si on trouve une planif d'ecf postérieure à la date d'inscription
                    queryPe = EntityConnection.GetContext().PlanningECF;
                    queryPe = queryPe.Where(it => (it.ECF.IdECF == this.IdECF && it.DateECF > pif.DateInscription));
                    queryPe = queryPe.OrderBy(it => it.DateECF);
                    pe = queryPe.FirstOrDefault();
                    if (pe != null)
                    {
                        return pe.DateECF;
                    }
                    else
                    {
                        // on cherche la date de fin du dernier module prérequis trouvé
                        IQueryable<PlanningIndividuelDetail> query = EntityConnection.GetContext().PlanningIndividuelDetail;
                        query = query.Where(it => it.CodePlanning == pif.CodePlanning);
                        query = query.OrderByDescending(it => it.FinCours);
                       /* PlanningIndividuelDetail pid = query.FirstOrDefault(null);*/
                        PlanningIndividuelDetail pid = null;
                        List<PlanningIndividuelDetail> listePlanningIndividuelDetail = query.ToList();
                        foreach(PlanningIndividuelDetail pidPif in listePlanningIndividuelDetail) {
                            ModuleParECF mpe = new ModuleParECF();
                            mpe.IdModule = pidPif.Cours.Module.IdModule;
                            if (this.ModuleParECF.Contains(mpe, new ModuleParECFComparerOnModule())&&pid==null)
                            {
                                pid = pidPif;
                            }
                        }
                        if (pid != null)
                        {
                            return pid.FinCours;
                        }
                        else
                        {
                            return null;
                        }
                    }
                 }
               
            }catch (Exception ex){
                throw ex;
            }
        }

        public DateTime? GetSuggestionDate(Promotion p)
        {
          try
          {
            // on cherche la date de fin du dernier module prérequis trouvé
            IQueryable<Cours> query = EntityConnection.GetContext().Cours;
            query = query.Where(it => it.CodePromotion == p.CodePromotion);
            query = query.OrderByDescending(it => it.Fin);
          /*  Cours c = query.FirstOrDefault(null);*/

            Cours c = null;
            List<Cours> listeCours = query.ToList();
            foreach (Cours cPromo in listeCours)
            {
                ModuleParECF mpe = new ModuleParECF();
                mpe.IdModule = cPromo.Module.IdModule;
                if (this.ModuleParECF.Contains(mpe, new ModuleParECFComparerOnModule()) && c == null)
                {
                    c = cPromo;
                }
            }
            if (c != null)
            {
                return c.Fin;
            }
            else
            {
                return null;
            }
          }
          catch (Exception ex)
          {
            throw ex;
          }
        }


        public void MiseAjour()
        {
            try
            {
                EntityConnection.GetContext().SaveChanges();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 
    }
}
